package com.springboot.handler;

import com.springboot.config.ProjectUrlConfig;
import com.springboot.exception.SellException;
import com.springboot.exception.SellerAuthorizeException;
import com.springboot.utils.ResultVOUtil;
import com.springboot.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 异常捕获类
 */
@ControllerAdvice
public class SellExceptionHandler {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {
        return new ModelAndView("redirect:"
                .concat(projectUrlConfig.getWechatOpenAuthorize())
                .concat("/sell/wechat/qrAuthorize")
                .concat("?returnUrl=")
                .concat(projectUrlConfig.getSell())
                .concat("/sell/seller/login"));
    }

    /**
     * 后台业务异常同意处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = SellException.class)
    public ResultVo handlerSellerException(SellException e) {
      return ResultVOUtil.error(e.getCode(),e.getMessage());
    }


}